home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / peek.c < prev    next >
Text File  |  1997-03-27  |  5KB  |  154 lines

  1. /*
  2.  * peek v1.0 - Brendan Kehoe (brendan@cs.widener.edu)
  3.  * Last changed: 12/17/90
  4.  *
  5.  * Peek in and see what directory a user's sitting in, along with
  6.  *  what they happen to be doing at the time, and if they've set
  7.  *  id to someone else. (An R means they're setuid to root, a '*'
  8.  *  means they're setuid to someone else.) Remember, many programs do
  9.  *  the setuid themselves [e.g. xterm], so don't freak if you see an R
  10.  *  there.
  11.  *
  12.  * This has only been used under SunOS 4.1. It probably won't work on
  13.  *  other systems. (But you can try .. let me know if it does!)
  14.  *
  15.  * Any comments, insults, fixes, whatever, are more than welcome.
  16.  *
  17.  * You must build this with -lkvm.
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <kvm.h>
  23. #include <fcntl.h>
  24. #include <pwd.h>
  25. #include <sys/param.h>
  26. #include <sys/time.h>
  27. #include <sys/proc.h>
  28. #include <sys/user.h>
  29.  
  30. static char *minus = "------------------------", *space = "                        ", *home = "~/";
  31.  
  32. #define MAXCOMWID       24      /* max "what" width */
  33.  
  34. main (argc, argv)
  35.      int argc;
  36.      char **argv;
  37. {
  38.   struct proc *p;
  39.   struct user *u;
  40.   struct passwd *w;
  41.   struct ucwd cwd;
  42.   static kvm_t *kd;
  43.   static char buf[100], outcmd[MAXCOMLEN + 1], *dirp, flag;
  44.   char *root = home + 1;
  45.   short homelen;
  46.  
  47.   if ((kd = kvm_open (NULL, NULL, NULL, O_RDONLY, "spy.dirs")) == NULL)
  48.     {
  49.       (void) fprintf (stderr, "%s:  couldn't open the kernel\n",
  50.                       *argv);
  51.       exit (1);
  52.     }
  53.  
  54.   printf ("\nName (*=Su)| What %s| ProcID | Current Directory\n\
  55. -----------+%s+--------+--------------------\n",
  56.           space + MAXCOMWID + 4 - MAXCOMLEN,
  57.           minus + MAXCOMWID - 2 - MAXCOMLEN);
  58.  
  59.   kvm_setproc (kd);
  60.   while ((p = kvm_nextproc (kd)))
  61.     /* only get active stuff */
  62.     if (p->p_stat != SZOMB && p->p_uid)
  63.       {
  64.         /*
  65.          * first read the u-area
  66.          */
  67.         if (!(u = kvm_getu (kd, p)))
  68.           continue;
  69.         /*
  70.          * now read in the u_cwd structure .. it's buried in the kernel
  71.          */
  72.         if (kvm_read (kd, (unsigned long) u->u_cwd, (struct ucwd *) & cwd,
  73.                       sizeof (struct ucwd)) < 0)
  74.           {
  75.             perror ("kvm_read structure");
  76.             exit (1);
  77.           }
  78.         /*
  79.          * and finally get the name of the directory; it's buried there too
  80.          *  but if it's bogus (they don't show up with one), leap on
  81.          */
  82.         if (!cwd.cw_dir)
  83.           break;
  84.  
  85.         if (kvm_read (kd, (unsigned long) cwd.cw_dir, buf, 100) < 0)
  86.           {
  87.             perror ("kvm_read dir");
  88.             exit (1);
  89.           }
  90.  
  91.         /*
  92.          * Read in their bio
  93.          */
  94.         w = getpwuid (p->p_uid);
  95.  
  96.         dirp = buf;
  97.  
  98.         if (!*dirp)
  99.           /*
  100.            * If the kernel-read didn't read anything for their current dir,
  101.            *  they're in the root directory
  102.            */
  103.           dirp = root;
  104.         else if (p->p_uid && *(w->pw_dir) && *(w->pw_dir + 1))
  105.           {
  106.             /*
  107.              *  Make sure that their home directory, if they have one,
  108.              *   isn't the root (/) directory. We won't do anything if
  109.              *   their home's / or they logged in without a home (e.g.
  110.              *   their home fs wasn't mounted at the time).
  111.              */
  112.             if (strlen (w->pw_dir) == strlen (dirp) && !strcmp (w->pw_dir, dirp))
  113.               /*
  114.                * If they're in their home directory, make it just "~/"
  115.                */
  116.               dirp = home;
  117.             else if (strncmp (buf, w->pw_dir, homelen = strlen (w->pw_dir)) == 0)
  118.               {
  119.                 /*
  120.                  * Ok, it's $HOME/blah, so make it ~/blah
  121.                  */
  122.                 *(dirp = buf + homelen - 1) = '~';
  123.                 *(dirp + 1) = '/';
  124.               }
  125.           }
  126.  
  127.         *outcmd = '\0';
  128.         if (strlen (u->u_comm) > MAXCOMLEN)
  129.           {
  130.             strncpy (outcmd, u->u_comm, MAXCOMLEN);
  131.             *(outcmd + MAXCOMLEN + 1) = '\0';
  132.           }
  133.  
  134.         flag = ' ';
  135.         if (!p->p_suid)
  136.           /*
  137.            * if they're root, scream it
  138.            */
  139.           flag = 'R';
  140.         else if (p->p_uid != p->p_suid)
  141.           /*
  142.            * otherwise it might be harmless; just mark it
  143.            */
  144.           flag = '*';
  145.         printf ("%*s%c | %*s | %6d%c| %*s\n",
  146.                 -9, w->pw_name,
  147.                 flag,
  148.                 -MAXCOMLEN, *outcmd ? outcmd : u->u_comm,
  149.                 p->p_pid,
  150.                 (p->p_flag & SLOGIN) ? 's' : ' ',
  151.                 -39, dirp);
  152.       }
  153. }
  154.